/*
 * Copyright (C) 2012 scchang <scchang@caltech.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ogame.utilities.planets;

import java.util.ArrayList;
import java.util.LinkedList;
import ogame.utilities.Coords;
import ogame.utilities.Player;
import ogame.utilities.datastorage.OgameDataStorage;

/**
 *
 * @author scchang <scchang@caltech.edu>
 */
public class Planets extends LinkedList<Planet>{
    
    ///////////////////////////////////////////////////////////////////////////
    /**
     * Returns without modifying the input array with the added planetID.
     * @param input
     * @param planetID
     * @return 
     */
    public static byte[] addPlanetToList(byte[] input, int planetID){
        ArrayList<Byte> theList = new ArrayList<>(input.length);
        
        for(int i = 0; i < input.length; ++i){
            theList.add(input[i]);
        }
        
        // Modify the number of planets
        int numPlanets = theList.get(0);
        numPlanets++;
        theList.set(0, new Byte(""+numPlanets));
        
        // Covert a planet id
        int count = 0;
        if(planetID != 0){
            count = (int)Math.floor(Math.log(planetID)/Math.log(256)) + 1;
        }else{
            count = 1;
        }
        theList.add(Byte.parseByte("" + count));
        for(int j = count - 1; j >= 0; --j){
            int mod = (int)Math.floor(planetID/(Math.pow(256, j)));
            planetID -= mod*Math.pow(256, j);
            mod -= 128;
            theList.add(Byte.parseByte("" + mod));
        }
        
        Byte[] resultByte = new Byte[theList.size()];
        resultByte = theList.toArray(resultByte);
        
        byte[] result = new byte[theList.size()];
        for(int i = 0; i < result.length; ++i){
            result[i] = resultByte[i];
        }
        return result;        
    }
    
    public String toString(){
        String result = "";
        for(Planet planet : this){
            result+=planet.toString();
        }
        return result;
    }
    
    public byte[] encode(OgameDataStorage ogameDataStorage){
        ArrayList<Byte> theList = new ArrayList<>();
        
        theList.add(Byte.parseByte("" + this.size()));
        
        for(Planet planet : this){
            // Get planet id
            int planet_id = ogameDataStorage.getPlanetTable().getPlanetID(planet);
            
            if(planet_id == -1){
                System.out.println("Unable to find a planet's id.");
                return null;
            }
            
            // Covert a planet id
            int count = 0;
            if(planet_id != 0){
                count = (int)Math.floor(Math.log(planet_id)/Math.log(256)) + 1;
            }else{
                count = 1;
            }
            theList.add(Byte.parseByte("" + count));
            for(int j = count - 1; j >= 0; --j){
                int mod = (int)Math.floor(planet_id/(Math.pow(256, j)));
                planet_id -= mod*Math.pow(256, j);
                mod -= 128;
                theList.add(Byte.parseByte("" + mod));
            }
        }
        Byte[] resultByte = new Byte[theList.size()];
        resultByte = theList.toArray(resultByte);
        
        byte[] result = new byte[theList.size()];
        for(int i = 0; i < result.length; ++i){
            result[i] = resultByte[i];
        }
        return result;
    }

    public static Planets parsePlanets(byte[] input,
            Player player, OgameDataStorage ogameDataStorage){
        if(input.length == 0){
            throw new IllegalArgumentException("Unable to parse planets since "
                    + "input length is zero.");
        }
        
        Planets output = new Planets();
        
        int numPlanets = input[0];
        int currentIndex = 1;
        
        for(int i = 0; i < numPlanets; ++i){
            int numContaining = input[currentIndex];
            int planet_id = 0;
            for(int j = 0; j < numContaining; ++j){
                currentIndex++;
                planet_id = input[currentIndex] + (256*planet_id) + 128;
            }
            
            output.add(ogameDataStorage.getPlanetTable().getPlanetFromID(planet_id));
            
            currentIndex++;
        }
        
        // Add player to all reulst and vice versa
        for(Planet planet: output){
            player.addPlanet(planet);
        }
        
        return output;
    }
}
